/*
 * HSM Proxy Project.
 * Copyright (C) 2013 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package be.fedict.hsm.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = Constants.TABLE_NAME_PREFIX + "sec_audit")
@NamedQueries({
		@NamedQuery(name = SecurityAuditEntity.QUERY_ALL, query = "SELECT sae FROM SecurityAuditEntity AS sae ORDER BY sae.id DESC"),
		@NamedQuery(name = SecurityAuditEntity.REMOVE_ALL, query = "DELETE FROM SecurityAuditEntity") })
public class SecurityAuditEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	public static final String QUERY_ALL = "sec_audit.all";

	public static final String REMOVE_ALL = "sec_audit.del.all";

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;

	@Temporal(TemporalType.TIMESTAMP)
	private Date created;

	private String subject;

	private String host;

	@Enumerated(EnumType.STRING)
	private SecurityEvent securityEvent;

	private boolean outcome;

	public SecurityAuditEntity() {
		super();
	}

	public SecurityAuditEntity(SecurityEvent securityEvent, String host,
			String subject, boolean outcome) {
		this.created = new Date();
		this.securityEvent = securityEvent;
		this.host = host;
		this.subject = subject;
		this.outcome = outcome;
	}

	public long getId() {
		return this.id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Date getCreated() {
		return this.created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public String getSubject() {
		return this.subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public SecurityEvent getSecurityEvent() {
		return this.securityEvent;
	}

	public void setSecurityEvent(SecurityEvent securityEvent) {
		this.securityEvent = securityEvent;
	}

	public boolean isOutcome() {
		return this.outcome;
	}

	public void setOutcome(boolean outcome) {
		this.outcome = outcome;
	}

	public String getHost() {
		return this.host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public static List<SecurityAuditEntity> listAll(EntityManager entityManager) {
		Query query = entityManager.createNamedQuery(QUERY_ALL);
		return query.getResultList();
	}

	public static int removeAll(EntityManager entityManager) {
		Query query = entityManager.createNamedQuery(REMOVE_ALL);
		return query.executeUpdate();
	}
}
